package main;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.Color;
import java.awt.Font;
//import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI implements ActionListener {
	
	private Simulation sim = new Simulation();
	private Experimentation exp = new Experimentation();
	private JPanel settings = new JPanel(), initial = new JPanel();
	private JFrame Program_Window = new JFrame("Senior Thesis Path Searching Program");
	private JButton generate_new_map = new JButton("Generate New Map"),
					start_search = new JButton("Start Search"),
					end_search = new JButton("End Search"),
					change_algorithm = new JButton("Switch"),
					visualBack = new JButton("Back"),
					goToVisual = new JButton("Visual Mode"),
					goToExperiment = new JButton("Experiment Mode");
	public static JButton experimentBack;
	private JRadioButton option_1 = new JRadioButton("2 Agents"),
						 option_2 = new JRadioButton("4  Agents"), 
						 option_3 = new JRadioButton("8 Agents");
	public static JLabel search_status = new JLabel(),
						 number_steps = new JLabel(),
						 elapsed_time = new JLabel();
	private ButtonGroup agent_group = new ButtonGroup();
	private int button_start_location = 75;
	private JLabel title = new JLabel("SWARM INTELLIGENCE ALGORITHM"), 
			       description = new JLabel("<html> The options below allow you to set<br>" +
			       									"the number of agents in the Swarm <br> " +
			       									"Intelligence Algorithm.");
	public static boolean isSwarm = true;
	
	public GUI()
	{
		setUpStartScreen();
		setUpVisualDisplay();
		setUpStandard();
	}
	public void setUpStandard()
	{
		Program_Window.setSize(1300, 628);
		Program_Window.setResizable(false);
		Program_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Program_Window.setLayout(null);
		Program_Window.setLocationRelativeTo(null);
		
		settings.setVisible(false);
		sim.setVisible(false);
		initial.setVisible(true);
		
		Program_Window.getContentPane().add(settings);
		Program_Window.getContentPane().add(sim);
		Program_Window.getContentPane().add(initial);
		Program_Window.getContentPane().add(exp);
		
		Program_Window.setVisible(true);
	}
	public void setUpVisualDisplay()
	{	
		title.setBounds(42, 15, 215, 50);
		
		generate_new_map.setActionCommand("Generate a new map");
		generate_new_map.addActionListener(this);
		generate_new_map.setBounds(75, button_start_location, 150, 50);
		
		start_search.setActionCommand("Begin searching for target location");
		start_search.addActionListener(this);
		start_search.setBounds(75, button_start_location+70, 150, 50);
		
		end_search.setActionCommand("Cancel current search");
		end_search.addActionListener(this);
		end_search.setBounds(75, button_start_location+140, 150, 50);
		
		change_algorithm.setActionCommand("Change");
		change_algorithm.setToolTipText("<HTML>Click to go to Artificial<br>Intelligence Algorithm");
		change_algorithm.addActionListener(this);
		change_algorithm.setBounds(210, 575, 80, 20);
		
		description.setBounds(42, button_start_location+150, 200, 200);
		
		option_1.setSelected(true);
		option_1.setActionCommand("1 Option");
		option_1.addActionListener(this);
		option_1.setBounds(75, button_start_location+280, 150, 50);
		option_1.setBackground(null);
		
		option_2.setActionCommand("2 Option");
		option_2.addActionListener(this);
		option_2.setBounds(75, button_start_location+330, 150, 50);
		option_2.setBackground(null);
		
		option_3.setActionCommand("3 Option");
		option_3.addActionListener(this);
		option_3.setBounds(75, button_start_location+380, 150, 50);
		option_3.setBackground(null);
		
		search_status.setBounds(42, button_start_location+420, 250, 50);
		search_status.setBackground(null);
		number_steps.setBounds(42, button_start_location+435, 250, 50);
		number_steps.setBackground(null);
		elapsed_time.setBounds(42, button_start_location+450, 400, 50);
		elapsed_time.setBackground(null);
		
		agent_group.add(option_1);
		agent_group.add(option_2);
		agent_group.add(option_3);
		
		visualBack.addActionListener(this);
		visualBack.setActionCommand("initial");
		visualBack.setBounds(10, 575, 80, 20);
		
		settings.setBounds(1000, 0, 300, 600);
		settings.setBackground(new Color (191, 0, 255));
		settings.setLayout(null);
		settings.add(title);
		settings.add(generate_new_map);
		settings.add(start_search);
		settings.add(end_search);
		settings.add(description);
		settings.add(search_status);
		settings.add(change_algorithm);
		settings.add(number_steps);
		settings.add(elapsed_time);
		settings.add(option_1);
		settings.add(option_2);
		settings.add(option_3);
		settings.add(visualBack);
	}
	public void setUpStartScreen()
	{
		initial.setBounds(0,0,1300,600);
		initial.setLayout(null);
		initial.setBackground(new Color (191, 0, 255));
		
		JLabel mainTitle = new JLabel("Senior Thesis Path Searching Program");
		mainTitle.setFont(new Font("Serif", Font.BOLD, 72));
		mainTitle.setBounds(53,0,1194,100);
		
		goToVisual.setActionCommand("Visual");
		goToVisual.addActionListener(this);
		goToVisual.setBounds(350,150,600,200);
		goToVisual.setFont(new Font("Serif", Font.BOLD, 72));
		goToVisual.setBackground(new Color (0, 191, 255));
		goToVisual.setToolTipText("<html><center><h1 style ='font-size: 24;'>Visual Display" +
				" of Path Searching Algorithm</h1>");
		
		goToExperiment.setActionCommand("Experiment");
		goToExperiment.addActionListener(this);
		goToExperiment.setBounds(350,375,600, 200);
		goToExperiment.setFont(new Font("Serif", Font.BOLD, 72));
		goToExperiment.setBackground(new Color (255, 0, 64));
		goToExperiment.setToolTipText("<html><center><h1 style ='font-size: 24;'>Allows User" +
				" to Conduct Experiments</h1>");
		
		experimentBack = new JButton("Back");
		experimentBack.setBounds(575, 394, 150, 50);
		experimentBack.addActionListener(this);
		experimentBack.setActionCommand("initial");
		experimentBack.setBackground(new Color (255, 0, 64));
		exp.add(experimentBack);
		
		initial.add(goToVisual);
		initial.add(goToExperiment);
		initial.add(mainTitle);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("Generate a new map"))
			sim.createNewMap();
		else if(e.getActionCommand().equals("Begin searching for target location"))
			sim.startSearching();
		else if(e.getActionCommand().equals("Cancel current search"))
			sim.endSearching();
		else if(e.getActionCommand().equals("1 Option"))
		{
			if(isSwarm)
				sim.setNumberAgents(2);
			else
				sim.setVisionRange(2);
		}
		else if(e.getActionCommand().equals("2 Option"))
		{
			if(isSwarm)
				sim.setNumberAgents(4);
			else
				sim.setVisionRange(3);
		}
		else if(e.getActionCommand().equals("3 Option"))
		{
			if(isSwarm)
				sim.setNumberAgents(8);
			else
				sim.setVisionRange(4);
		}
		else if (e.getActionCommand().equals("Change"))
		{
			if(!sim.searching)
			{
				isSwarm = !isSwarm;
				changeSettings();
				option_1.doClick();
				sim.setSearchStatus(SearchStatus.NOTSEARCHING);
			}
		}
		else if(e.getActionCommand().equals("Visual"))
		{
			sim.setVisible(true);
			settings.setVisible(true);
			initial.setVisible(false);
		}
		else if(e.getActionCommand().equals("Experiment"))
		{
			exp.setVisible(true);
			initial.setVisible(false);
		}
		else if(e.getActionCommand().equals("initial"))
		{
			if(!sim.searching)
			{
				if (exp.isVisible())
				{
					exp.setVisible(false);
					initial.setVisible(true);
				}
				else
				{
					sim.setVisible(false);
					settings.setVisible(false);
					initial.setVisible(true);
				}
			}
		}
	}
	public void changeSettings()
	{
		if(isSwarm)
		{
			title.setText("SWARM INTELLIGENCE ALGORITHM");
			change_algorithm.setToolTipText("<HTML>Click to go to Artificial<br>Intelligence Algorithm");
			description.setText("<html> The options below allow you to set<br>" +
									 	     "the number of agents in the Swarm <br> " +
									         "Intelligence Algorithm.");
			option_1.setText("2 Agents");
			option_2.setText("4 Agents");
			option_3.setText("8 Agents");
		}
		else
		{
			title.setText("ARTIFICIAL INTELLIGENCE ALGORITHM");
			change_algorithm.setToolTipText("<HTML>Click to go to Swarm<br>Intelligence Algorithm");
			description.setText("<html> The options below allow you to set<br>" +
			       						"the vision range of the agent in the<br>" +
			       						"Artificial Intelligence Algorithm.");
			option_1.setText("Vision Range of 2");
			option_2.setText("Vision Range of 3");
			option_3.setText("Vision Range of 4");
			
		}
	}
}